# import cv2
# import os
# from tqdm import tqdm

# def draw_lines(image_path, gt_path, pred_path, output_path):
#     # 读取图像
#     image = cv2.imread(image_path)

#     # 读取并绘制GT数据
#     with open(gt_path, 'r') as f:
#         gt_lines = f.readlines()
#         for line in gt_lines:
#             points = [max(0, int(float(x))) for x in line.strip().split()]
#             for i in range(0, len(points) - 1, 2):
#                 if i + 3 < len(points):  # 确保有足够的点来画线
#                     cv2.line(image, (points[i], points[i + 1]), (points[i + 2], points[i + 3]), (255, 0, 0), 2)  # 蓝色线

#     # 读取并绘制预测数据
#     with open(pred_path, 'r') as f:
#         pred_lines = f.readlines()
#         for line in pred_lines:
#             points = [max(0, int(float(x))) for x in line.strip().split()]
#             for i in range(0, len(points) - 1, 2):
#                 if i + 3 < len(points):  # 确保有足够的点来画线
#                     cv2.line(image, (points[i], points[i + 1]), (points[i + 2], points[i + 3]), (0, 0, 255), 2)  # 红色线

#     # 在图像上标注行数
#     font = cv2.FONT_HERSHEY_SIMPLEX
#     cv2.putText(image, f'GT Lines: {len(gt_lines)}', (10, 30), font, 1, (255, 0, 0), 2, cv2.LINE_AA)
#     cv2.putText(image, f'Pred Lines: {len(pred_lines)}', (10, 70), font, 1, (0, 0, 255), 2, cv2.LINE_AA)

#     # 保存图像
#     cv2.imwrite(output_path, image)


# # 定义输入输出路径
# test_file = '/root/autodl-tmp/Culane/list/test.txt'
# output_folder = '/root/autodl-tmp/New'

# # 读取 test.txt 文件
# with open(test_file, 'r') as f:
#     lines = f.readlines()

# # 处理每一行
# # for line in lines:
# for line in tqdm(lines):
#     line = line.strip()
#     if not line.endswith('.jpg'):
#         continue

#     image_path = '/root/autodl-tmp/Culane' + line
#     base_name = os.path.splitext(line)[0]
#     gt_path = '/root/autodl-tmp/Culane'+ base_name + '.lines.txt'
#     pred_path = '/home/cc/AAA_czk/c_kan_ciouloss/tmp'+ line.replace('.jpg', '.lines.txt')

#     # 创建输出文件夹（如果不存在）
#     output_dir = os.path.join(output_folder, os.path.dirname(line))
#     os.makedirs(output_dir, exist_ok=True)

#     # 处理图像并保存
#     output_path = os.path.join(output_folder, line)
#     draw_lines(image_path, gt_path, pred_path, output_path)

# print("处理完成！")

####*********************多线程处理版本

import cv2
import os
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor

# 定义输入输出路径
test_file = '/root/autodl-tmp/Culane/list/test.txt'
output_folder = '/home/cc/NewPic'

def draw_lines(image_path, gt_path, pred_path, output_path):
    # 读取图像
    image = cv2.imread(image_path)
    # image 剪裁到 [0, 1640, 270, 590]  x_min x_max y_min y_max
    # image = image[170:490, 0:1640]

    # 读取并绘制GT数据
    with open(gt_path, 'r') as f:
        gt_lines = f.readlines()
        for line in gt_lines:
            points = [max(0, int(float(x))) for x in line.strip().split()]
            for i in range(0, len(points) - 1, 2):
                if i + 3 < len(points):  # 确保有足够的点来画线
                    cv2.line(image, (points[i], points[i + 1]), (points[i + 2], points[i + 3]), (255, 0, 0), 2)  # 蓝色线

    # 读取并绘制预测数据
    with open(pred_path, 'r') as f:
        pred_lines = f.readlines()
        for line in pred_lines:
            points = [max(0, int(float(x))) for x in line.strip().split()]
            for i in range(0, len(points) - 1, 2):
                if i + 3 < len(points):  # 确保有足够的点来画线
                    cv2.line(image, (points[i], points[i + 1]), (points[i + 2], points[i + 3]), (0, 0, 255), 2)  # 红色线

    # 在图像上标注行数
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(image, f'GT Lines: {len(gt_lines)}', (10, 30), font, 1, (255, 0, 0), 2, cv2.LINE_AA)
    cv2.putText(image, f'Pred Lines: {len(pred_lines)}', (10, 70), font, 1, (0, 0, 255), 2, cv2.LINE_AA)

    # 保存图像
    cv2.imwrite(output_path, image)

def process_line(line):
    line = line.strip()
    if not line.endswith('.jpg'):
        return

    image_path = '/root/autodl-tmp/Culane' + line
    base_name = os.path.splitext(line)[0]
    gt_path = '/root/autodl-tmp/Culane' + base_name + '.lines.txt'
    pred_path = '/home/cc/AAA_python_test/tmp/tmp_20250222_1654/' + line.replace('.jpg', '.lines.txt')

    # 创建输出文件夹（如果不存在）
    output_dir = os.path.join(output_folder, os.path.dirname(line))
    os.makedirs(output_dir, exist_ok=True)

    # 处理图像并保存
    output_path = os.path.join(output_folder, line)
    draw_lines(image_path, gt_path, pred_path, output_path)



# 读取 test.txt 文件
with open(test_file, 'r') as f:
    lines = f.readlines()

# 使用 ThreadPoolExecutor 进行多线程处理
# 使用 ProcessPoolExecutor 进行多进程处理
with ProcessPoolExecutor() as executor:
    list(tqdm(executor.map(process_line, lines), total=len(lines)))

print("处理完成！")